class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        int step = words[0].size();
        vector<int> ret;
        unordered_map<string, int> hash1;
        for (auto str : words)
            hash1[str]++;

        for (int i = 0; i < step; i++)
        {
            int count = 0;
            unordered_map<string, int> hash2;
            for (int left = i, right = i; right + step <= s.size(); right += step)
            {
                string in = s.substr(right, step);
                if (hash1.find(in) != hash1.end() && ++hash2[in] <= hash1[in])   count++;
                if (right - left + 1 > words.size() * step)
                {
                    string out = s.substr(left, step);
                    if (hash1.find(out) != hash1.end() && hash2[out]-- <= hash1[out])  count--;
                    left += step;
                }
                if (count == words.size())
                {
                    ret.push_back(left);
                }

            }
        }
        return ret;
    }
};